package com.itheima.web; /**
 * @author tongchen
 * @create 2022-12-01 16:35
 */

import com.itheima.pojo.User;
import com.itheima.service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //创建对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        UserService userService = new UserService();
        boolean register = userService.register(user);
        if(register==false){
            //携带错误信息跳转
            request.setAttribute("register_msg","注册失败，用户名已存在");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        }else{

            //跳转到登录页面
            request.setAttribute("register_msg","注册成功，请登录");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
